package com.ss.android.common.util;

import com.bytedance.common.utility.Logger;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public final class IdCache {
    public static final class Id {
        public Long id;
        public long time;

        public Id() {

        }

        public final boolean equals(Object arg3) {
            boolean v0 = !(arg3 instanceof Id) || arg3 == null ? super.equals(arg3) : this.id.equals(((Id) arg3).id);
            return v0;
        }

        public final int hashCode() {
            return this.id.hashCode();
        }

        public final String toString() {
            return String.valueOf(this.id) + "|" + String.valueOf(this.time);
        }
    }

    private final int mMaxCount;
    private final TreeMap<Id, Id> mIds;

    public IdCache(int arg3) {
        this.mIds = new TreeMap(new i());
        this.mMaxCount = arg3;
    }

    public final String saveIds() {
        String v0_2 = null;
        String v1;
        synchronized (this) {
            try {
                v1 = "";
                StringBuilder v3 = new StringBuilder();
                Set v4 = this.mIds.entrySet();
                Iterator<Map.Entry<Id, Id>> v5 = v4.iterator();
                int v2;
                for (v2 = 0; v5.hasNext(); ++v2) {
                    Id v0_1 = v5.next().getValue();
                    if (v0_1 == null) {
                        continue;
                    }

                    if (v2 != v4.size() - 1) {
                        v3.append(((Id) v0_1).toString()).append("@");
                    } else {
                        v3.append(((Id) v0_1).toString());
                    }
                }
                v0_2 = v3.toString();
                if (Logger.debug()) {
                    Logger.d("IdCache", "saveIds : " + v0_2);
                }
                return v0_2;
            } catch (Throwable v0) {
                v0.printStackTrace();
            }
        }
        return "";
    }

    public final void loadIds(String arg9) {
        int v0 = 0;
        synchronized (this) {
            if (android.support.design.a.isTextEmpty(arg9)) {
                return;
            }

            if (Logger.debug()) {
                Logger.d("IdCache", "loadIds : " + arg9);
            }

            try {
                this.mIds.clear();
                String[] v1 = arg9.split("@");
                if (v1 == null) {
                    return;
                }

                int v2 = v1.length;
                while (v0 < v2) {
                    String v3 = v1[v0];
                    Id id = new Id();
                    if (v3 != null) {
                        String[] v3_1 = v3.split("\\|");
                        if (v3_1 != null && v3_1.length == 2) {
                            id.id = Long.valueOf(v3_1[0]);
                            id.time = Long.valueOf(v3_1[1]).longValue();
                        }
                    }
                    this.addId(id);
                    ++v0;
                }
            } catch (Exception v0_2) {
                v0_2.printStackTrace();
            }
        }
    }

    public final boolean isIdExist(Id id) {
        boolean v0;
        synchronized (this) {
            if (id == null) {
                v0 = false;
            } else {
                v0 = this.mIds.containsKey(id);
                if (Logger.debug()) {
                    Logger.d("IdCache", "isidExist : " + v0);
                }
            }
            return v0;
        }
    }

    public final Id getId(Id arg6) {
        boolean v1_2;
        Object v0_3 = null;
        Id v0;
        Id v1 = null;
        synchronized (this) {
            if (arg6 == null) {
                v0 = v1;
                return null;
            }

            try {
                v0_3 = this.mIds.get(arg6);
                v1_2 = Logger.debug();
                if (!v1_2) {
                    return (Id) v0_3;
                }

                if (v0_3 == null) {
                    return (Id) v0_3;
                }
                Logger.d("IdCache", "getId : " + v0_3.toString());
                return (Id) v0_3;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return ((Id) v0_3);
        }
    }

    public final void addId(Id id) {
        synchronized (this) {
            if (id != null) {
                try {
                    if (Logger.debug()) {
                        Logger.d("IdCache", "addId : " + id.toString());
                    }

                    if (Logger.debug()) {
                        Logger.d("IdCache", "before removeIds");
                        this.saveIds();
                    }

                    if (this.mIds.size() >= this.mMaxCount && !this.isIdExist(id)) {
                        if (Logger.debug()) {
                            Logger.d("IdCache", "removeId : " + this.mIds.get(this.mIds.firstKey()).toString());
                        }

                        this.mIds.remove(this.mIds.firstKey());
                    }

                    if (this.isIdExist(id)) {
                        if (Logger.debug()) {
                            Logger.d("IdCache", "removeId : " + this.getId(id).toString());
                        }

                        this.mIds.remove(id);
                    }

                    this.mIds.put(id, id);
                    if (Logger.debug()) {
                        Logger.d("IdCache", "after removeIds");
                        this.saveIds();
                    }

                    if (Logger.debug()) {
                        Logger.d("IdCache", "Ids size : " + this.mIds.size());
                    }


                } catch (Exception v0_1) {
                    v0_1.printStackTrace();
                }
            }
        }
    }
}

